// var datas=require("../../data/data.js")
var my = require("../../utils/MyUtils.js")
var log=require("../../utils/log")
const app = getApp()
Page({
  data: {
    infoMess: '',
    userName: '',
    userN: '',
    passWd: '',
    passW: '',
    system: null,
    token: {}
  },
  //用户名和密码输入框事件
  userNameInput: function (e) {
    try {
      this.setData({
        userN: e.detail.value
      })
    } catch (e) {
      wx.showToast({
        title: '手机号格式不对',
        icon: 'none'
      })
    }
  },
  passWdInput: function (e) {
    this.setData({
      passW: e.detail.value
    })
  },
  //登录按钮点击事件，调用参数要用：this.data.参数；
  //设置参数值，要使用this.setData({}）方法
  loginBtnClick: function (e) {
    app.globalData.test=e
    var that = this
    if (this.data.userN.length == 0) {
      wx.showToast({
        title: '用户名不能为空',
        icon: 'none'
      })
    } else if (this.data.userN.length < 11 || isNaN(Number(this.data.userN))) {
      wx.showToast({
        title: '手机号格式不对',
        icon: 'none'
      })
    } else if (this.data.passW.length == 0) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none'
      })
    } else if (this.data.passW.length < 6) {
      wx.showToast({
        title: '密码不低于6个字符',
        icon: 'none'
      })
    } else {
      var system = null
      wx.getSystemInfo({
        complete: (res) => {
          if (res.model.indexOf("iP") == 0) {
            that.setData({
              system: "IOS"
            })
          } else if (res.model.indexOf("ANDROID") == 0) {
            that.setData({
              system: "ANDROID"
            })
          }
        },
      })
      var ss = {
        "userN": this.data.userN,
        "passW": this.data.passW,
        "system": this.data.system
      }
      my.logins(e)
      setTimeout(function(){
        var open_id = wx.getStorageSync('OPEN_ID');
      my.request('/oa-applet/login', "post", {
        user_openid: open_id
      }, {
        "phone": ss.userN,
        "pwd": ss.passW,
        "equipment": ss.system
      }, function (res) {
        if (res.data.code == 0) {
          wx.setStorageSync('_TOKEN', res.data.data.token)
          log.info(my.info(new Date(),ss.userN,"登录"))
          wx.reLaunch({
            url: '../my/my',
          })
          
        } else {
          my.alert(res.data.message)
          return false
        }
      })
      },1000)  
    }
  },
  onLoad: function () {
  },
  quick: function () {
    wx.navigateTo({
      url: '../login/forget/index',
    })
  }
})